package cn.liwq.study.demo.netty.lab.mahjong.client;

import cn.liwq.study.demo.netty.lab.mahjong.client.handler.MahjongClientHandler;
import cn.liwq.study.demo.netty.lab.mahjong.common.codec.MahjongFrameDecoder;
import cn.liwq.study.demo.netty.lab.mahjong.common.codec.MahjongFrameEncoder;
import cn.liwq.study.demo.netty.lab.mahjong.common.codec.MahjongProtocolDecoder;
import cn.liwq.study.demo.netty.lab.mahjong.common.codec.MahjongProtocolEncoder;
import cn.liwq.study.demo.netty.lab.mahjong.common.dto.HelloRequest;
import cn.liwq.study.demo.netty.lab.mahjong.common.protocol.MahjongProtocol;
import cn.liwq.study.demo.netty.lab.mahjong.common.protocol.MahjongProtocolHeader;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.BasicConfigurator;

import java.net.InetSocketAddress;

/**
 * @author lweiqiang
 * @date 2021/5/8
 */
@Slf4j
public class MahjongClient {
    public static void main(String[] args) throws InterruptedException {
        BasicConfigurator.configure();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        try {
            bootstrap
                    .group(workerGroup)
                    .channel(NioSocketChannel.class)
                    .handler(
                            new ChannelInitializer<SocketChannel>() {
                                @Override
                                protected void initChannel(SocketChannel ch) throws Exception {
                                    ChannelPipeline pipeline = ch.pipeline();
                                    pipeline.addLast(new LoggingHandler(LogLevel.DEBUG));
                                    // 一次编解码
                                    pipeline.addLast(new MahjongFrameDecoder());
                                    pipeline.addLast(new MahjongFrameEncoder());
                                    // 二次编解码
                                    pipeline.addLast(new MahjongProtocolDecoder());
                                    pipeline.addLast(new MahjongProtocolEncoder());

                                    pipeline.addLast(new MahjongClientHandler());
                                }
                            });
            ChannelFuture f = bootstrap.connect(new InetSocketAddress("127.0.0.1", 8017)).sync();
            log.info("connect to server success");

            // 连接建立完成之后发送hello消息给服务端
            HelloRequest helloRequest = new HelloRequest();
            helloRequest.setName("tt");

            MahjongProtocolHeader header = new MahjongProtocolHeader();
            header.setVersion(1);
            header.setReqId(1);
            header.setCmd(1);

            MahjongProtocol mahjongProtocol = new MahjongProtocol();
            mahjongProtocol.setHeader(header);
            mahjongProtocol.setBody(helloRequest);

            f.channel().writeAndFlush(mahjongProtocol);

            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
        }
    }
}
